<?php
ini_set("date.timezone", "Asia/Chongqing");

$prog=new Program();
$prog->run();

class Program
{
    private $plain;

    public function __construct()
    {
        $this->plain=<<<EOD
登快阁 黄庭坚（宋）
------------------
痴儿了却公家事，快阁东西倚晚晴。
落木千山天远大，澄江一道月分明。
朱弦已为佳人绝，青眼聊因美酒横。
万里归船弄长笛，此心吾与白鸥盟。
EOD
;
    }

    public function run()
    {
        self::test_mcrypt();
    }

    private function test_mcrypt()
    {
        print "test_mcrypt\n";
        print "--------------------------------------------------\n";

        print "Mode:\n";
        $modes=mcrypt_list_modes();
        foreach($modes as $mode)
        {
            print $mode."\n";
        }
        print "--------------------------------------------------\n";

        print "Cipher:\n";
        $ciphers=mcrypt_list_algorithms();
        foreach($ciphers as $cipher)
        {
            print $cipher."\n";
        }
        print "--------------------------------------------------\n";

        print "AES:\n";

        $key="1234567890"."123456";
        $algorithm=MCRYPT_RIJNDAEL_128;
        $mode=MCRYPT_MODE_ECB;

        //$iv=mcrypt_create_iv(mcrypt_get_iv_size($algorithm, $mode),
        //        MCRYPT_RAND);
        //$enc=mcrypt_encrypt($algorithm, $key, $this->plain, $mode, $iv);
        //print Misc::ExpandToString($enc)."\n";
        //$iv=mcrypt_create_iv(mcrypt_get_iv_size($algorithm, $mode),
        //        MCRYPT_RAND);
        //$plain2=mcrypt_decrypt($algorithm, $key, $enc, $mode, $iv);
        //$plain2=rtrim($plain2, "\x00");

        // PKCS5Padding
        //$enc=Misc::CollapseFromString("3BD83B29F5AF71BF79270F5DD607ACBABCE23C7E94A722E180481FD88F8BF46B226602F3C45030DB717D66B620B1DF4B94476AFD25E342049021997698EA8078949376B45D6DEC36B2439C629A4CADE739AF60F81616D0CFD369DCA0320E52D6F872283D6E53052D3BE81BFD0967BF5806835E845C5EEF2EBE2B71B0565B89BF59CFF174E56F8D90AD076C90558D8FB9D2666D72032C2982F568F19F6985E456B8839039B8BF0794B3F1A83538ADCC38353DD7FF4C270E3DF7346F908383A6620517A2956F11BCBCA2A02BE01A329090C41AEB9B8EA8B111749ABFA677A523AD0C944B9ED3262D1C0D8A3C489ED29C30");
        $enc=Misc::CollapseFromString("E5F45EC2E45F5BB4D0387039033864C81BCAE13335D460A33F6CFB81480AD3D5217EC124C0F98F8E2C21C7179E6519C1EFE66493C87598E1BEFE6C21B2905FAA74B8B097C73B132168E7DDA5E631D097893DDCA5C22C87CF5EEF21DE772B716323F15B2454FBC457CFEB0D7243913ED6CBA24D6FC004F72E65EE4A08181E644A8F029D4C131ABBE50FAFEBD6A9141CB3");
        $plain2=mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $key, $enc,
                MCRYPT_MODE_ECB);
        $len=strlen($plain2);
        $padding=ord($plain2[$len-1]);
        echo "Padding: $padding\n";
        $plain2=substr($plain2, 0, -$padding);

        print "($plain2)\n";

        $file=fopen("mcrypt_aes_tmp.txt", "w");
        fwrite($file, $plain2);
        fwrite($file, "\n");
        fclose($file);

        print "==================================================\n";
    }
}

class Misc
{
    public static function ExpandToString($bin)
    {/*{{{*/
        $str="";
        foreach(str_split($bin, 1) as $c)
        {
            $str.=sprintf("%02X", ord($c));
        }
        return $str;
    }/*}}}*/

    public static function CollapseFromString($str)
    {/*{{{*/
        $bin="";
        $len=strlen($str);
        if($len % 2 == 0)
        {
            for($i=0; $i<$len; $i+=2)
            {
                $bin.=chr(intval(substr($str, $i, 2), 16));
            }
        }
        return $bin;
    }/*}}}*/
}
?>
